'Copyright (c) 2009 Grailtek

'Permission is hereby granted, free of charge, to any person
'obtaining a copy of this software and associated documentation
'files (the "Software"), to deal in the Software without
'restriction, including without limitation the rights to use,
'copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the
'Software is furnished to do so, subject to the following
'conditions:

'The above copyright notice and this permission notice shall be
'included in all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'OTHER DEALINGS IN THE SOFTWARE.

Imports System.Collections.Generic
Imports System.Aspect

Partial Structure Contract

    <Serializable()> _
    Public Class Parameter
        Implements IEquatable(Of Parameter)
        Implements IComparable(Of Parameter)

        Friend Sub New()
        End Sub
        Public Sub New(ByVal iRegisterName As String, ByVal iType As String)
            If iType Is Nothing Then Throw New ArgumentNullException("Type")
            Name = iRegisterName
            Type = iType
        End Sub
        Public Sub New(ByVal iRegisterName As String, ByVal iType As Type)
            If iType Is Nothing Then Throw New ArgumentNullException("Type")
            Name = iRegisterName
            Type = iType.Name
            LocalType = iType
        End Sub
        'TODO: need to refactor this
        Public Sub New(ByVal i As InputMapping)
            Name = i.Name
            If String.IsNullOrEmpty(Name) Then Name = i.Name
            Dim Nullable As Boolean = i.FieldType.IsGenericType AndAlso i.FieldType.GetGenericTypeDefinition() Is GetType(Nullable(Of ))
            Type = If(Nullable, i.FieldType.GetGenericArguments.First.Name + "?", i.FieldType.Name)
            LocalType = i.FieldType
        End Sub
        Public Sub New(ByVal o As OutputMapping)
            Name = o.FieldAttribute.RegisterName
            If String.IsNullOrEmpty(Name) Then Name = o.Name
            Dim Nullable As Boolean = o.FieldType.IsGenericType AndAlso o.FieldType.GetGenericTypeDefinition() Is GetType(Nullable(Of ))
            Type = If(Nullable, o.FieldType.GetGenericArguments.First.Name + "?", o.FieldType.Name)
            LocalType = o.FieldType
        End Sub
        Public Name, Type As String
        Public ReadOnly LocalType As Type
        Public Overloads Function Equals(ByVal other As Parameter) As Boolean Implements System.IEquatable(Of Parameter).Equals
            Return Me.Name = other.Name AndAlso Me.Type = other.Type
        End Function

        Public Function CompareTo(ByVal other As Parameter) As Integer Implements System.IComparable(Of Parameter).CompareTo
            Return String.Compare(Me.Name, other.Name)
        End Function

        Public Shared Function CompareSets(ByVal p1 As Parameter(), ByVal p2 As Parameter()) As Boolean
            If p1 Is Nothing Then Return p2 Is Nothing
            If p2 Is Nothing Then Return False
            If p1.Length <> p2.Length Then Return False
            If p1.Length = 0 Then Return True
            Dim p2params As New List(Of Parameter)(p2)
            For Each p As Parameter In p1
                If Not p2params.Contains(p) Then Return False
            Next
            Return True
        End Function

        Public Const SigNode As String = "Signature"

        Public Shared Sub SerialzeSet(ByVal writer As System.Xml.XmlWriter, ByVal params() As Parameter)
            writer.WriteStartElement(SigNode)
            Dim items As New List(Of String)
            For Each p As Parameter In params
                If p.Name Is Nothing Then
                    items.Add(p.Type)
                Else
                    items.Add(p.Name & " " & p.Type)
                End If
            Next
            writer.WriteRaw(String.Join(", ", items.ToArray))
            writer.WriteEndElement()
        End Sub

        Public Shared Function DeserialzeSet(ByVal xml As String) As Parameter()
            Dim params As New List(Of Parameter)
            For Each item As String In xml.Split(","c)
                Dim atoms() As String = item.Trim.Split(" "c)
                Dim Name As String = Nothing
                Dim TypeName As String = Nothing
                If atoms.Length = 2 Then
                    Name = atoms(0)
                    TypeName = atoms(1)
                Else
                    TypeName = atoms(0)
                End If
                If Not (String.IsNullOrEmpty(Name) AndAlso String.IsNullOrEmpty(TypeName)) Then
                    Dim p As New Parameter(Name, TypeName)
                    params.Add(p)
                End If
            Next
            Return params.ToArray
        End Function

    End Class

End Structure